 
package view;

import openhome.bo.DispositivosBO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

 
public class Dispositivos extends javax.swing.JInternalFrame {
 
    public Dispositivos() {
        initComponents();
        consultar();
    }
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jPanel1 = new javax.swing.JPanel();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnActualizar = new javax.swing.JButton();
        btnExportar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnSalir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDispositivos = new javax.swing.JTable();

        setBackground(new java.awt.Color(204, 204, 204));
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Dispositivos");

        jToolBar1.setBackground(new java.awt.Color(0, 0, 0));
        jToolBar1.setRollover(true);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setLayout(new java.awt.GridLayout());

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        jPanel1.add(btnNuevo);

        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        jPanel1.add(btnEditar);

        btnEliminar.setText("Eliminar");
        jPanel1.add(btnEliminar);

        btnActualizar.setText("Actualizar");
        btnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarActionPerformed(evt);
            }
        });
        jPanel1.add(btnActualizar);

        btnExportar.setText("Exportar XLS");
        btnExportar.setFocusable(false);
        btnExportar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnExportar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jPanel1.add(btnExportar);

        jPanel2.setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 95, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 23, Short.MAX_VALUE)
        );

        jPanel1.add(jPanel2);

        btnSalir.setText("Salir");
        btnSalir.setFocusable(false);
        btnSalir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });
        jPanel1.add(btnSalir);

        jToolBar1.add(jPanel1);

        tbDispositivos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDispositivos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDispositivosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDispositivos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
         try {
           DispositivosEdit popup = new DispositivosEdit(null,true);
           popup.setTitle(popup.getTitle() + "- Nuevo");
           popup.setLocation(this.getWidth() - popup.getWidth(),this.getHeight()/2 - popup.getHeight()/3);
           popup.setVisible(true);
        } catch (Exception e) {
        }
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
         editar();
    }//GEN-LAST:event_btnEditarActionPerformed

    private void tbDispositivosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDispositivosMouseClicked
        if(evt.getClickCount()==2){
            editar();            
        }
    }//GEN-LAST:event_tbDispositivosMouseClicked

    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed
        consultar();
    }//GEN-LAST:event_btnActualizarActionPerformed

   public void editar(){
       int fila = tbDispositivos.getSelectedRow();       
       if (fila>=0){
            DispositivosEdit popup = new DispositivosEdit(null,true);
            popup.setLocation(this.getWidth() - popup.getWidth(),this.getHeight()/2 - popup.getHeight()/3);
            popup.setTitle(popup.getTitle() + " - Edición");                  
            popup.setIddispositivo(Integer.parseInt(tbDispositivos.getValueAt(fila, 0).toString()));
            popup.setVisible(true);  
       }
   }
            
    public void consultar(){
        
        Map param = new HashMap();
        try {
            DispositivosBO dispo = new DispositivosBO();
        
            ResultSet rs = (ResultSet)dispo.traerDispositivos();
            DefaultTableModel z = new DefaultTableModel();
            z.addColumn("ID");
            z.addColumn("DISPOSITIVO");
            z.addColumn("PIN ARDUINO");
            z.addColumn("PWD");
            z.addColumn("TIPO");
                      
            Object[] fila = new Object[5];
            while(rs.next()){                
                fila[0]= rs.getInt("iddispositivo");
                fila[1]= rs.getString("dispositivo");
                fila[2]= rs.getInt("pin_arduino");
                fila[3]= rs.getInt("pwd");
                fila[4]= rs.getString("tipo_dispositivo");                
                z.addRow(fila);
            }            
            tbDispositivos.setModel(z);
            tbDispositivos.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            
            //tbDispositivos.getColumnModel().getColumn(0).setMaxWidth(0);
            //tbDispositivos.getColumnModel().getColumn(0).setResizable(true);
            //tbDispositivos.setEditable(false);
            //tbPersonal.getColumnExt(0).setVisible(false);           
        } catch (Exception ex) {
          Logger.getLogger(DispositivosBO.class.getName()).log(Level.SEVERE, null, ex);
        }        
}
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualizar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnExportar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnSalir;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tbDispositivos;
    // End of variables declaration//GEN-END:variables
}


